<?php
/**
 * @Author: Xu Liu email:2867557054@qq.com
 * @Date:   2016-12-15 13:31:33
 * @Last Modified by:   Xu Liu
 * @Last Modified time: 2017-12-02 13:45:57
 */
if (!defined('IN_ADMINCP') || !defined('IN_DISCUZ')) {
    exit('Access Denied');
}
class MbookContrl
{
    public function index()
    {
        global $_G;
        $currentpage = $_GET['page']?intval($_GET['page']):1;
        $pagesize = 50;
        if ($keybookname = trim($_GET['book_name'])) {
            if (xfxs_isutf($keybookname) && strtolower(CHARSET) != 'utf-8') {
                $keybookname = diconv($keybookname, 'utf-8', CHARSET);
            } else if (!xfxs_isutf($keybookname) && strtolower($keybookname) == 'utf-8') {
                $keybookname = diconv($keybookname, 'GB2312', CHARSET);
            }
            $counts   = C::t('#jameson_read#jamesonread_books')->count_search_at_author($_G['uid'], $keybookname, 1);
            $turl = ADMINSCRIPT.'?action=plugins&operation=config&do=' . $_GET['do'] . '&identifier=jameson_read&pmod=jameson_read&contrl=mbook&act=index&book_name=' . $keybookname;
            $books    = C::t('#jameson_read#jamesonread_books')->fetch_search_at_author($_G['uid'], $keybookname, $pagesize*($currentpage-1),$pagesize);
        } else {
            $cou_con = $fet_con = $order = array();
            if($_GET['category_id']){
                $cou_con = array('category_id','=',$_GET['category_id']);
                $fet_con =array($_GET['category_id']);
            }
            if($_GET['order']){
                $order[0] = 'b.'.($_GET['byfield']?$_GET['byfield']:'addtime').' '.$_GET['order'];
            }
            $counts = C::t('#jameson_read#jamesonread_books')->count_by_con($cou_con,1);
            if ($counts) {
                $turl = ADMINSCRIPT.'?action=plugins&operation=config&do=' . $_GET['do'] . '&identifier=jameson_read&pmod=jameson_read&contrl=mbook&act=index'.($_GET['category_id']?'&category_id='.$_GET['category_id']:'').($_GET['byfield']?'&byfield='.$_GET['byfield']:'').'&order='.$_GET['order'];
                $books    = C::t('#jameson_read#jamesonread_books')->fetchAll_by_field(array(),$fet_con,$order,$pagesize*($currentpage-1),$pagesize , 1);
                if($books && count($books) < $pagesize){
                    $nobids = array();
                    foreach ($books as $b) {
                        $nobids[] = $b['book_id'];
                    }
                    $allcate = C::t('#jameson_read#jamesonread_categorys')->fetch_categoryid();
                    $nofenleibooks = DB::fetch_all('SELECT * FROM %t WHERE  '.DB::field('category_id',$allcate,'notin').' AND  '.DB::field('book_id',$nobids,'notin'),array('jamesonread_books'),'book_id');
                    $books = array_merge($books,$nofenleibooks);
                }
            }
        }
        $pageshow = multi($counts,$pagesize,$currentpage,$turl);
        // 全部一级分类
        $categorys = C::t('#jameson_read#jamesonread_categorys')->fetch_all_cate();
        $forumlist = DB::fetch_all('SELECT * FROM %t WHERE status <> %d ORDER BY fup',array('forum_forum',3),'fid');
        foreach ($forumlist as $f => $fm) {
            if($fm['type']=='forum'){
                $forumlist[$fm['fup']]['sub'][$f]=$fm;
                unset($forumlist[$f]);
            }
        }
        $qunzulist = DB::fetch_all('SELECT * FROM %t WHERE status=%d ORDER BY fup',array('forum_forum',3),'fid');
        $qzs = array();
        foreach ($qunzulist as $key => $value) {
        	if($value['type']=='group'){
        		$qzs[$key] = $value;
        	}else if($value['type']=='forum'){
        		$qzs[$value['fup']]['sub'][$key]=$value;
        	}else if($value['type']=='sub'){
        		$qzs[$qunzulist[$value['fup']]['fup']]['sub'][$value['fup']]['qunzu'][$key] = $value;
        	}
        }
        include template('jameson_read:mbook_index');
    }
    // 书评管理
    public function shuping()
    {
        global $_G;
        $currentpage = $_GET['page']?intval($_GET['page']):1;
        $pagesize = 30;
        $counts = C::t('#jameson_read#jamesonread_dianping')->count_all();
        if ($counts) {
            // 全部一级分类
            $shuping  = C::t('#jameson_read#jamesonread_dianping')->fetch_all_shuping($pagesize*($currentpage-1), $pagesize);
            $pageshow = multi($counts,$pagesize,$currentpage,ADMINSCRIPT.'?action=plugins&operation=config&do=' . $_GET['do'] . '&identifier=jameson_read&pmod=jameson_read&contrl=mbook&act=shuping');
        }
        include template('jameson_read:mbook_shuping');
    }
    public function post()
    {
        global $_G;
        if (submitcheck('editsubmit')) {
            if ($_GET['delbook'] && is_array($_GET['delbook'])) {
                foreach ($_GET['delbook'] as $key => $value) {
                    C::t('#jameson_read#jamesonread_books')->delete(intval($key));
                    jamesonread_deletebook($key);
                    unset($_GET['name'][$key]);
                    C::t('#jameson_read#jamesonread_books')->clear_cache(array("bookinfo-".$key."-".$_G['uid'], "bookinfo-".$key."-0"));
                }
            }
            foreach ($_GET['name'] as $key => $value) {
                if (trim($value)) {
                    $data                = array();
                    $data['book_name']   = daddslashes(dhtmlspecialchars($value));
                    if(isset($_GET['price'][$key])){
                        $data['bookmoney']   = intval($_GET['price'][$key]);
                    }
                    $data['category_id'] = intval($_GET['category_id'][$key][$_GET['parent_id'][$key]]);
                    $data['parent_id']   = intval($_GET['parent_id'][$key]);
                    $data['is_top']      = intval($_GET['is_top'][$key]);
                    $data['zishu']       = intval($_GET['zishu'][$key]);
                    $data['sign'] = intval($_GET['sign'][$key]);
                    $data['is_tuwen']    = 1;
                    $data['bindfid']     = intval($_GET['forum'][$key]);
                    $data['qzfid']     = intval($_GET['qunzu'][$key]);
                    $data['scores']      = round($_GET['scores'][$key] * $_GET['dpcount'][$key] / 2, 1);
                    C::t('#jameson_read#jamesonread_books')->update($key, $data);
                    C::t('#jameson_read#jamesonread_books')->clear_cache(array("bookinfo-".$key."-".$_G['uid'], "bookinfo-".$key."-0"));
                }
            }
            C::t('#jameson_read#jamesonread_books')->clear_cache(array('jamesonreadnewtuijian','jamesonreadnewpaihang'));
            cpmsg(lang('plugin/jameson_read', 'gengxinchenggong'), 'action=plugins&operation=config&do=' . $_GET['do'] . '&identifier=jameson_read&pmod=jameson_read&contrl=mbook&act=index', 'succeed');
        } else {
            cpmsg(lang('plugin/jameson_read', 'laiyuanfeifa'), 'action=plugins&operation=config&do=' . $_GET['do'] . '&identifier=jameson_read&pmod=jameson_read&contrl=mbook&act=index', 'error');
        }
    }
// 删除操作
    public function del()
    {
        global $_G;
        if ($_GET['forward']) {
            $forward = 'xiajia';
        } else {
            $forward = '';
        }
        $book_id = intval($_GET['book_id']);
        if (FORMHASH != trim($_GET['formhash']) || !$book_id) {
            cpmsg(lang('plugin/jameson_read', 'laiyuanfeifa'), 'action=plugins&operation=config&do=' . $_GET['do'] . '&identifier=jameson_read&pmod=jameson_read&contrl=mbook&act=' . $forward . 'index', 'error');
        } else {
            C::t('#jameson_read#jamesonread_books')->delete($book_id);
            jamesonread_deletebook($book_id);
            C::t('#jameson_read#jamesonread_books')->clear_cache(array("bookinfo-".$book_id."-".$_G['uid'], "bookinfo-".$book_id."-0"));
            cpmsg(lang('plugin/jameson_read', 'shanchuchenggong'), 'action=plugins&operation=config&do=' . $_GET['do'] . '&identifier=jameson_read&pmod=jameson_read&contrl=mbook&act=' . $forward . 'index', 'succeed');
        }
    }
    // 删除操作
    public function delshuping()
    {
        global $_G;
        if (FORMHASH != trim($_GET['formhash'])) {
            cpmsg(lang('plugin/jameson_read', 'laiyuanfeifa'), 'action=plugins&operation=config&do=' . $_GET['do'] . '&identifier=jameson_read&pmod=jameson_read&contrl=mbook&act=shuping', 'error');
        } else {
            foreach ($_GET['dpid'] as $key => $value) {
                C::t('#jameson_read#jamesonread_dianping')->delete(intval($value));
            }
            cpmsg(lang('plugin/jameson_read', 'shanchuchenggong'), 'action=plugins&operation=config&do=' . $_GET['do'] . '&identifier=jameson_read&pmod=jameson_read&contrl=mbook&act=shuping', 'succeed');
        }
    }
// 下架操作
    public function xiajia()
    {
        global $_G;
        if ($_GET['forward']) {
            $forward = 'xiajia';
            $message = lang('plugin/jameson_read', 'yishangjia');
            $type    = 1;
        } else {
            $forward = '';
            $message = lang('plugin/jameson_read', 'yixiajia');
            $type    = 0;
        }
        if (FORMHASH != trim($_GET['formhash'])) {
            cpmsg(lang('plugin/jameson_read', 'laiyuanfeifa'), 'action=plugins&operation=config&do=' . $_GET['do'] . '&identifier=jameson_read&pmod=jameson_read&contrl=mbook&act=' . $forward . 'index', 'error');
        } else {
            $book_id = intval($_GET['book_id']);
            C::t('#jameson_read#jamesonread_books')->update($book_id, array('status' => $type));
            C::t('#jameson_read#jamesonread_books')->clear_cache(array("bookinfo-".$book_id."-".$_G['uid'], "bookinfo-".$book_id."-0"));
            cpmsg($message, 'action=plugins&operation=config&do=' . $_GET['do'] . '&identifier=jameson_read&pmod=jameson_read&contrl=mbook&act=' . $forward . 'index', 'succeed');
        }
    }
    // 下架图书管理页面
    public function xiajiaindex()
    {
        global $_G;
        $currentpage = $_GET['page']?intval($_GET['page']):1;
        $pagesize = 50;
        $counts = C::t('#jameson_read#jamesonread_books')->count_online(0);
        if ($counts) {
            $books = C::t('#jameson_read#jamesonread_books')->fetch_online($pagesize*($currentpage-1), $pagesize, 0);
            foreach ($books as $key => $value) {
                // 当前图书的一级分类id
                $books[$key]['category_name'] = C::t('#jameson_read#jamesonread_categorys')->fetch_name($value['category_id']);
            }
            $pageshow = multi($counts,$pagesize,$currentpage,ADMINSCRIPT.'?action=plugins&operation=config&do=' . $_GET['do'] . '&identifier=jameson_read&pmod=jameson_read&contrl=mbook&act=xiajiaindex');
        }
        include template('jameson_read:mbook_xiajiaindex');
    }
    function guanli(){
        global $_G;
        $book_id = intval($_GET['book_id']);
        $books                  = C::t('#jameson_read#jamesonread_books')->fetchAllInfoById($book_id);
        include template('jameson_read:mbook_guanli');
    }
}